package com.onlinesshop.mall.controller;

import com.onlinesshop.mall.pojo.ShopCart;
import com.onlinesshop.mall.pojo.User;
import com.onlinesshop.mall.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 叶建雄
 * @date 2019/12/25 18:18
 */
@Controller
@RequestMapping("/shopcart")
    public class ShopCartController {

    @Autowired
    private ShopCartService shopCartService;
    /**
     * 获取用户添加所有的商品
     * @param request
     * @param modelAndView
     * @return
     */
    @RequestMapping("/toshop_cart")
    public ModelAndView toShopCart(ShopCart shopCart,HttpServletRequest request , ModelAndView modelAndView) {
            //从session中获取用户对象
            User user = (User) request.getSession().getAttribute("user");
            int id = user.getId();
            //获取需要加入加入购物车的商品id
            String productId = request.getParameter("productId");
            if(productId != null) {
                shopCart.setProduct_id(Integer.parseInt(productId));
                shopCart.setUser_id(id);
                ShopCart byShopCart = shopCartService.findByShopCart(shopCart);

                //判断该该商品是否已经加入购物车
                if (byShopCart == null) {
                    //如果没有添加该商品
                    shopCart.setQuantity(1);
                    shopCart.setChecked(0);
                    shopCartService.saveByShopCart(shopCart);

                } else {
                    //如果已经添加，则商品数量加一
                    shopCartService.updateByShopCart(shopCart);
                }
            }
        //通过用户ID获取用户添加的商品
        List<ShopCart> shopCartList = shopCartService.findAllShopByUserID(id);
            Iterator<ShopCart> iterator = shopCartList.iterator();
            double totalprice=0;
            while(iterator.hasNext()){
                ShopCart next = iterator.next();
                double price = next.getProduct().getPrice();
                int quantity = next.getQuantity();
                double sligtotal= price * (double)quantity;
                totalprice= sligtotal +totalprice;
            }
            request.getSession().setAttribute("productsize",shopCartList.size());
            modelAndView.addObject("totalprice",totalprice);
            modelAndView.addObject("shopCartList",shopCartList);

        modelAndView.setViewName("shop_cart");

        return modelAndView;
    }

    @RequestMapping("/msTopay")
    public ModelAndView msTopay(ModelAndView modelAndView,HttpServletRequest request){
     /*   int productsize = (int) request.getSession().getAttribute("productsize");
        List<Integer> idList = new ArrayList<>();
        for(int i=0;i<productsize-1;i++){
            String proId = request.getParameter(String.valueOf(i));
            if(proId!=null){
                int id = Integer.parseInt(proId);
                idList.add(id);
            }
        }*/
        User user = (User) request.getSession().getAttribute("user");
        if(user != null){
            //获取用户对象
            int id = user.getId();
            //通过用户ID获取用户添加的商品
            List<ShopCart> shopCartList = shopCartService.findAllShopByUserID(id);
            modelAndView.addObject("shopCartList",shopCartList);
            modelAndView.setViewName("Orders");
        }
        //通过产品id将产品选中
      //  shopCartService.updateFlagByProIds(idList);
        return modelAndView;
    }
}
